package hu.uszeged.inf.stud.prf.todo.entity.usertask;

import hu.uszeged.inf.stud.prf.todo.entity.task.Task;
import hu.uszeged.inf.stud.prf.todo.entity.task.TaskType;
import hu.uszeged.inf.stud.prf.todo.entity.user.User;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "user_task")
public class UserTask implements Serializable {
	
	private static final long serialVersionUID = -5635645173372982375L;

	@Id
	@SequenceGenerator(name = "idGenSeqUserTask", initialValue=30)
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "idGenSeqUserTask")
	private Long id;
	
	@ManyToOne
	private User user;
	
	@ManyToOne
	private Task task;

	@ManyToMany(cascade = CascadeType.ALL)
	@JoinTable(name = "usertask_tasktype",
			joinColumns = @JoinColumn(name = "usertask_id"),
			inverseJoinColumns = @JoinColumn(name = "tasktype_id")
		)
	private List<TaskType> tasktypes;

	private boolean state;
	
	private boolean archive;

	public UserTask() {
		super();
		this.state = false;
	}

	public UserTask(Long id, User user, Task task, List<TaskType> tasktypes,
			boolean state) {
		super();
		this.id = id;
		this.user = user;
		this.task = task;
		this.tasktypes = tasktypes;
		this.state = state;
	}

	public UserTask(User user, UserTask usertask) {
		super();
		this.user = user;
		this.task = usertask.getTask();
		this.tasktypes = usertask.getTasktypes();
		this.state = usertask.getState();
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Task getTask() {
		return task;
	}

	public void setTask(Task task) {
		this.task = task;
	}

	public List<TaskType> getTasktypes() {
		return tasktypes;
	}

	public void setTasktypes(List<TaskType> tasktypes) {
		this.tasktypes = tasktypes;
	}

	public boolean getState() {
		return state;
	}

	public void setState(boolean state) {
		this.state = state;
	}

	public boolean isArchive() {
		return archive;
	}

	public void setArchive(boolean archive) {
		this.archive = archive;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((state = false) ? 0 : 1);
		result = prime * result + ((task == null) ? 0 : task.hashCode());
		result = prime * result
				+ ((tasktypes == null) ? 0 : tasktypes.hashCode());
		result = prime * result + ((user == null) ? 0 : user.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		UserTask other = (UserTask) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (state != other.state)
			return false;
		if (task == null) {
			if (other.task != null)
				return false;
		} else if (!task.equals(other.task))
			return false;
		if (tasktypes == null) {
			if (other.tasktypes != null)
				return false;
		} else if (!tasktypes.equals(other.tasktypes))
			return false;
		if (user == null) {
			if (other.user != null)
				return false;
		} else if (!user.equals(other.user))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "UserTask [id=" + id + ", user=" + user + ", task=" + task
				+ ", tasktypes=" + tasktypes + ", state=" + state + "]";
	}	

	
}